﻿using System;
using System.IO;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Windows.Forms;

namespace CrmVersionhandlerTFS
{
    public class TFS
    {
        private VersionControlServer versionControl;
        private Workspace workspace;
        private string name = string.Empty;

        public TFS(string url, string username, string password, string domain)
        {
            name = username;
            TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(url), new NetworkCredential(name, password, domain));
            tfs.Connect(ConnectOptions.IncludeServices);
            versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
        }

        public bool CreateWorkspace(string sourceControlPath, string localPath)
        {
            Workspace[] workspaces = versionControl.QueryWorkspaces(SystemInformation.ComputerName, name, SystemInformation.ComputerName);
            if (workspaces.Length == 0)
            {
                CreateWorkSpace(sourceControlPath, localPath);
            }
            else
            {
                workspace = workspaces[0];
            }

            return true;
        }

        internal void CreateWorkSpace(string sourceControlPath, string localPath)
        {
            if (!Directory.Exists(localPath))
            {
                Directory.CreateDirectory(localPath);
            }
            WorkingFolder[] workingFolders = new WorkingFolder[] {
                new WorkingFolder(sourceControlPath, localPath, WorkingFolderType.Map)
                };
            versionControl.CreateWorkspace(SystemInformation.ComputerName, name, String.Empty, workingFolders, SystemInformation.ComputerName, true);
        }

        public bool Update(string localFile, string localFolder)
        {
            GetStatus status = workspace.Get();

            if (!File.Exists(localFile))
                return false;

            if (status.HaveResolvableWarnings)
            {
                workspace.ResolveConflict(workspace.QueryConflicts(new string[] { localFile }, true)[0]);
            }

            int numberOfFiles = workspace.PendEdit(localFile);
            if (numberOfFiles == 0)
                return false;


            return true;
        }

        public bool AddIfNotAlreadyInSvn(string customizationfolder)
        {
            int numberOfFiles = workspace.PendAdd(customizationfolder, true);
            return true;
        }

        public bool Commit()
        {
            PendingChange[] pendingChanges = workspace.GetPendingChanges();
            if (pendingChanges.Length > 0)
            { 
                int numberOfFiles = workspace.CheckIn(pendingChanges, "Crm versionhandler commit");
            }

            return true;
        }
    }
}
